home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / btrieve / winbtr / sample.c < prev    next >
Text File  |  1979-11-29  |  12KB  |  313 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* SAMPLE.c                                                                */
  4. /*                                                                         */
  5. /*   Sample MS-Windows program which opens a btrieve file and allows a     */
  6. /*   limited number of operations to be performed on the file              */
  7. /*                                                                         */
  8. /*                                                                         */
  9. /***************************************************************************/
  10. /***************************************************************************/
  11. #include <windows.h>
  12. #include "sample.h"
  13. #include "globals.h"
  14.  
  15. /* main initialization routine   */
  16. extern int initSample (HANDLE, HANDLE, int);
  17.  
  18. /* Dialog Procedures  */
  19. extern BOOL far pascal AboutBoxWindowProc (HWND,unsigned,WORD,LONG);
  20. extern BOOL far pascal OpenFileBoxWindowProc (HWND,unsigned,WORD,LONG);
  21. extern BOOL far pascal BtrvOpsListBoxWindowProc (HWND,LONG);
  22.  
  23. /* Btrieve DLL interface functions */
  24. extern int SHELLINIT (char far *);
  25. extern int BTRVINIT (char far *);
  26. extern int BTRVSTOP (void);
  27. extern unsigned BTRV (int, char far *, char far *, unsigned far *, char far *, int);
  28.  
  29. /* Main message handler procedure */
  30. long far pascal WndProc (HWND, unsigned, WORD, LONG);
  31.  
  32.  
  33. /* Starting entry point  */
  34. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  35.     HANDLE   hInstance, hPrevInstance;
  36.     LPSTR    lpszCmdLine;
  37.     int      nCmdShow;
  38. {
  39.     MSG         msg;
  40.     int      btrInitStat;
  41.     char     errMsg[200];
  42.  
  43.     if (!initSample (hInstance, hPrevInstance, nCmdShow))
  44.        return (FALSE);
  45.  
  46. /****** Uncomment if initialization is desired.  Set your parameters in
  47.         the call.  *****/
  48. /*
  49.     if (btrInitStat = BTRVINIT ((char far *) "/m:48 /p:2048 /t:c:\\tmp\\btrieve.trn"))
  50.     {
  51.        sprintf (errMsg, "Error on BTRVINIT : %x", btrInitStat);
  52.        MessageBox (NULL, errMsg, " ERROR ", MB_OK);
  53.        DestroyWindow (parentWnd);
  54.        return (FALSE);
  55.     }
  56. */
  57.  
  58.     while (GetMessage (&msg, NULL, 0,0))
  59.     {
  60.        if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg))
  61.        {
  62.           TranslateMessage (&msg);
  63.           DispatchMessage (&msg);
  64.        }
  65.     }
  66.     return (msg.wParam);
  67. }
  68.  
  69.  
  70. LONG FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  71.      HWND      hWnd;
  72.      unsigned  iMessage;
  73.      WORD      wParam;
  74.      LONG      lParam;
  75. {
  76.      unsigned i, j, k;
  77.      static short xChar, yChar, xClient, yClient;
  78.      static short nVscrollPos, nHscrollPos;
  79.      static short nVscrollMax, nHscrollMax;
  80.      short        nHscrollInc, nVscrollInc;
  81.      short        nPaintBeg, nPaintEnd;
  82.      HDC          hDC;
  83.      PAINTSTRUCT  ps;
  84.      TEXTMETRIC   tm;
  85.      FARPROC      lpprocAddress;
  86.      FARPROC      OpsProcAddress;
  87.  
  88.  
  89.      switch (iMessage)
  90.      {
  91.         case WM_COMMAND :            /* All WM_COMMAND messages are internal */
  92.              switch (wParam)         /* to this application                  */
  93.              {
  94.                  case SAMPLE_ABOUT   :
  95.                       lpprocAddress = MakeProcInstance (AboutBoxWindowProc,
  96.                                                         parentInst);
  97.                       DialogBox (parentInst, "ABOUTBOX", parentWnd, lpprocAddress);
  98.                       FreeProcInstance (lpprocAddress);
  99.                       break;
  100.  
  101.                  case SAMPLE_EXECUTE    :
  102.                       textlines [0][0] = 0;
  103.                       textlines [2][0] = 0;
  104.                       textlines [3][0] = 0;
  105.  
  106.                       if ((posBlk = GlobalLock (hPosBlk)) == NULL)
  107.                       {
  108.                          MessageBox (NULL, "ERROR - Could not lock POSBLK",
  109.                                     " ERROR ", MB_OK);
  110.                          break;
  111.                       }
  112.                       btrstat = BTRV (op, (char far *) posBlk,
  113.                                       (char far *) dataBuf,
  114.                                       (unsigned far *) &dataLen,
  115.                                       (char far *) keyBuf, keyNum);
  116.  
  117.                       GlobalUnlock (hPosBlk);
  118.                       sprintf (textlines[0], "Btrieve Status for %s = %d", Operations[opNdx], btrstat);
  119.                       if (((op >= 5) && (op <= 13)) && (btrstat == 0))
  120.                       {
  121.                          strcpy (textlines [2], "Key     Record");
  122.                          sprintf (textlines [3], "%d  : %s", *((int *) dataBuf), &dataBuf[2]);
  123.                       }
  124.                       InvalidateRect (hWnd, NULL, TRUE);
  125.                       break;
  126.  
  127.                  case SAMPLE_OPENFILE:
  128.                       if (fileIsOpen)
  129.                       {
  130.                          SendMessage (hDlgModeless, WM_COMMAND, SAMPLE_OPCLOSE, 0L);
  131.                          FreeProcInstance (OpsProcAddress);
  132.                       }
  133.                       lpprocAddress = MakeProcInstance (OpenFileBoxWindowProc,
  134.                                                         parentInst);
  135.                       DialogBox (parentInst, "BTROPENBOX", parentWnd, lpprocAddress);
  136.                       FreeProcInstance (lpprocAddress);
  137.                       if (fileIsOpen)
  138.                       {
  139.                          dataLen = 100;
  140.                          OpsProcAddress = MakeProcInstance (
  141.                                           BtrvOpsListBoxWindowProc,
  142.                                           parentInst);
  143.                          hDlgModeless = CreateDialog (parentInst,
  144.                                         "BTRVOPSBOX", parentWnd,
  145.                                          OpsProcAddress);
  146.                       }
  147.                       break;
  148.  
  149.                  default:
  150.                       break;
  151.               }
  152.               break;
  153.  
  154.                                         /* All following messages are sent  */
  155.                                         /* to the application by MS-Windows */
  156.  
  157.         case WM_CREATE  :               /* Sent during CreateWindow ()      */
  158.              hDC = GetDC (hWnd);
  159.              GetTextMetrics (hDC, &tm);
  160.              xChar = tm.tmAveCharWidth;
  161.              yChar = tm.tmHeight + tm.tmExternalLeading;
  162.              ReleaseDC (hWnd, hDC);
  163.              if ((hPosBlk = GlobalAlloc (GHND, (LONG) 128)) == NULL)
  164.                 MessageBox (NULL, "ERROR - Could not allocate POSBLK",
  165.                            " ERROR ", MB_OK);
  166.              break;
  167.  
  168.         case WM_SIZE    :               /* Sent when window is re-sized    */
  169.              yClient = HIWORD (lParam);
  170.              xClient = LOWORD (lParam);
  171.  
  172.              nVscrollMax = max (0, NUMLINES + 2 - yClient / yChar);
  173.              nVscrollPos = min (nVscrollPos, nVscrollMax);
  174.              nHscrollMax = max (0, MAXWIDTH + 2 - xClient / xChar);
  175.              nHscrollPos = min (nHscrollPos, nHscrollMax);
  176.  
  177.              SetScrollRange (hWnd, SB_VERT, 0, nVscrollMax, FALSE);
  178.              SetScrollPos   (hWnd, SB_VERT, nVscrollPos, TRUE);
  179.              SetScrollRange (hWnd, SB_HORZ, 0, nHscrollMax, FALSE);
  180.              SetScrollPos   (hWnd, SB_HORZ, nHscrollPos, TRUE);
  181.              break;
  182.  
  183.  
  184.         case WM_VSCROLL :               /* Sent when vertical scroll bar  */
  185.              switch (wParam)            /* is clicked up or down          */
  186.              {
  187.                  case SB_TOP      :
  188.                       nVscrollInc = -nVscrollPos;
  189.                       break;
  190.  
  191.                  case SB_BOTTOM   :
  192.                       nVscrollInc = nVscrollMax - nVscrollPos;
  193.                       break;
  194.  
  195.                  case SB_LINEUP   :
  196.                       nVscrollInc = -1;
  197.                       break;
  198.  
  199.                  case SB_LINEDOWN :
  200.                       nVscrollInc = 1;
  201.                       break;
  202.  
  203.                  case SB_PAGEUP   :
  204.                       nVscrollInc = min (-1, -yClient / yChar);
  205.                       break;
  206.  
  207.                  case SB_PAGEDOWN :
  208.                       nVscrollInc = max (1, yClient / yChar);
  209.                       break;
  210.  
  211.                  case SB_THUMBTRACK :
  212.                       nVscrollInc = LOWORD (lParam) - nVscrollPos;
  213.                       break;
  214.  
  215.                  default :
  216.                       nVscrollInc = 0;
  217.                       break;
  218.              }
  219.              if (nVscrollInc = max (-nVscrollPos,
  220.                                min (nVscrollInc, nVscrollMax - nVscrollPos)))
  221.              {
  222.                 nVscrollPos += nVscrollInc;
  223.                 ScrollWindow (hWnd, 0, -yChar * nVscrollInc, NULL, NULL);
  224.                 SetScrollPos (hWnd, SB_VERT, nVscrollPos, TRUE);
  225.                 UpdateWindow (hWnd);
  226.              }
  227.              break;
  228.  
  229.  
  230.         case WM_HSCROLL :           /* Sent when horizontal scroll bar is  */
  231.              switch (wParam)        /* clicked right or left               */
  232.              {
  233.                 case SB_LINEUP   :
  234.                      nHscrollInc = -1;
  235.                      break;
  236.  
  237.                 case SB_LINEDOWN :
  238.                      nHscrollInc = 1;
  239.                      break;
  240.  
  241.                 case SB_PAGEUP   :
  242.                      nHscrollInc = -8;
  243.                      break;
  244.  
  245.                 case SB_PAGEDOWN  :
  246.                      nHscrollInc = 8;
  247.                      break;
  248.  
  249.                 case SB_THUMBPOSITION :
  250.                      nHscrollInc = LOWORD (lParam) - nHscrollPos;
  251.                      break;
  252.  
  253.                 default :
  254.                      nHscrollInc = 0;
  255.              }
  256.              if (nHscrollInc = max (-nHscrollPos,
  257.                                min (nHscrollInc, nHscrollMax - nHscrollPos)))
  258.              {
  259.                 nHscrollPos += nHscrollInc;
  260.                 ScrollWindow (hWnd, -xChar * nHscrollInc, 0, NULL, NULL);
  261.                 SetScrollPos (hWnd, SB_HORZ, nHscrollPos, TRUE);
  262.              }
  263.              break;
  264.  
  265.  
  266.         case WM_KEYDOWN :               /* Sent when a key is pressed     */
  267.              for (i = 0; i < NUMKEYS; i++)
  268.              {
  269.                 if (wParam == keyScroll [i].wVirtKey)
  270.                 {
  271.                    SendMessage (hWnd, keyScroll [i].iMessage,
  272.                                 keyScroll [i].wRequest, 0L);
  273.                    break;
  274.                 }
  275.              }
  276.              break;
  277.  
  278.         case WM_PAINT   :              /* Sent to re-paint the screen    */
  279.              BeginPaint (hWnd, &ps);
  280.              nPaintBeg = max (0, nVscrollPos + ps.rcPaint.top / yChar -1);
  281.              nPaintEnd = min (NUMLINES,
  282.                               nVscrollPos + ps.rcPaint.bottom / yChar);
  283.  
  284.              for (i = nPaintBeg; i < nPaintEnd; i++)
  285.                 TextOut (ps.hdc, xChar * (1 - nHscrollPos),
  286.                          yChar * (1 - nVscrollPos + i),
  287.                          &textlines[i][0], strlen (&textlines[i][0]));
  288.  
  289.              EndPaint (hWnd, &ps);
  290.              break;
  291.  
  292.         case WM_CLOSE   :          /* Sent when system menu CLOSE is */
  293.                                    /* selected                       */
  294.              if (fileIsOpen)       /* Close file if left open        */
  295.              {
  296.                 posBlk = GlobalLock (hPosBlk);
  297.                 BTRV (1, posBlk, dataBuf, &dataLen, keyBuf, 0);
  298.              }
  299.              GlobalFree (hPosBlk);
  300.  
  301.              BTRVSTOP ();          /* Terminate session with Btrieve */
  302.  
  303.         case WM_DESTROY :
  304.              PostQuitMessage (0);
  305.              break;
  306.  
  307.         default:                   /* All other messages handled by the */
  308.                                    /* default Window handler            */
  309.              return (DefWindowProc (hWnd, iMessage, wParam, lParam));
  310.       }
  311. return ((long) 0);
  312. }
  313.